package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<Employee>();
        qw.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(qw);
        if(one==null){
            return R.error("用户名不存在");
        }
        String password = employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(one.getPassword())){
            return R.error("密码错误");
        }
        if(one.getStatus()!=1){
            return R.error("账号已被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }
    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success(null);
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        Long id = (Long)request.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean save = employeeService.save(employee);
        return save?R.success("新增员工成功"):R.error("失败");
    }
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Employee> pageInit =new Page<>();
        pageInit.setCurrent(page);
        pageInit.setSize(pageSize);
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Employee::getName,name);
        employeeService.page(pageInit,qw);
        return R.success(pageInit);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        employee.setUpdateUser(employee.getId());
        employee.setUpdateTime(LocalDateTime.now());
        boolean b = employeeService.updateById(employee);
        if(b){
            return R.success("修改成功");
        }
        else {
            return R.error("失败");
        }
    }
    @GetMapping("/{id}")
    public R<Employee> edit(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
